<?php
	// -----------   Page Vars    ----------
	$to_root = '';
	$authorized = array(0, 1);
	
	define('ERROR_WRONG_SID', 100);
	define('ERROR_WRONG_PARAMETER', 101);
	define('ERROR_SESSION_TERMINATED', 102);
	define('ERROR_WRONG_ACTION', 103);
	
	//  ---- init
	$GLOBALS['ajax']='j';
	include($to_root.'_classes/init.php');
	
	if(isset($_SESSION['ajax_sID'])){
		if(isset($_POST['sID']) && $_POST['sID'] == $_SESSION['ajax_sID']){
			if(isset($_POST['action']) && $_POST['action']=='getUsers'){ 					//-------------   Get User select
				$count = $GLOBALS['Db-I']->getUserCount();
				$return = $GLOBALS['Db-I']->getUsers(-1, -1, 0, $count);
				if(!isset($return['Error'])){
					$main = new ViewDescriptor('content/parts/select_user');
					foreach($return as $entry) {
						//print_r($entry);
						$m = new SubViewDescriptor('option');
						$m->addValue('id', $entry->getId());
						$m->addValue('value', $entry->getName());
						
						$main->addSubView($m);
						unset($m);
					}
					echo $main->render();
				} else echo 'Error:'.$return['Error'];
				
			} elseif(isset($_POST['action']) && $_POST['action']=='getProjects'){ 					//-------------   Get Project select
				//$count = $GLOBALS['Db-I']->getProjectCount();
				//print_r($count);
				$return = $GLOBALS['Db-I']->getListTitles(-1, -1, 'asc');
				if(!isset($return['Error'])){
					$main = new ViewDescriptor('content/parts/select_project');
					foreach($return as $entry) {
						//print_r($entry);
						$m = new SubViewDescriptor('option');
						$m->addValue('id', $entry['id']);
						$m->addValue('value', $entry['title']);
						
						$main->addSubView($m);
						unset($m);
					}
					echo $main->render();
				} else echo 'Error:'.$return['Error'];
			} else if(isset($_POST['action']) && $_POST['action']=='addProject' &&isset($_POST['pID']) && isset($_POST['tID'])){ //-------------   Add Project to Todo
				$id = $_POST['tID'];
				$p = $_POST['pID'];
				
				$return = $GLOBALS['Db-I']->addTodoToList($p, $id);
				if(!isset($return['Error'])){
					$p = $GLOBALS['Db-I']->getProject($p);
					echo $p->getTitle();
				} else echo 'Error:'.$return['Error'];
			} else if(isset($_POST['action']) && $_POST['action']=='deleteProject' &&isset($_POST['pID']) && isset($_POST['tID'])){ //-------------   Delete Project from Todo
				$id = $_POST['tID'];
				$p = $_POST['pID'];
				$return = $GLOBALS['Db-I']->removeTodoFromList($p, $id);
				if(!isset($return['Error'])){
					echo 'ok';
				} else echo 'Error:'.$return['Error'];
				
			} else if(isset($_POST['action']) && $_POST['action']=='addUser' &&isset($_POST['uID']) && isset($_POST['tID'])){ //-------------   add User to Todo
				$id = $_POST['tID'];
				$u = $_POST['uID'];
				
				$return = $GLOBALS['Db-I']->addUserToTodo($id, $u, 'c');
				if(!isset($return['Error'])){
					$u = $GLOBALS['Db-I']->getUserById($u);
					echo $u->getName();
				} else echo 'Error:'.$return['Error'];
				
			} else if(isset($_POST['action']) && $_POST['action']=='deleteUser' &&isset($_POST['uID']) && isset($_POST['tID'])){ //-------------   Delete User from Todo
				$id = $_POST['tID'];
				$u = $_POST['uID'];
				$return = $GLOBALS['Db-I']->removeUserFromTodo($id, $u);
				if(!isset($return['Error'])){
					echo 'ok';
				} else echo 'Error:'.$return['Error'];
				
			} else if(isset($_POST['action']) && $_POST['action']=='addUserToList' &&isset($_POST['uID']) && isset($_POST['lID'])){ //-------------   add User to List
				$id = $_POST['lID'];
				$u = $_POST['uID'];

				$return = $GLOBALS['Db-I']->shareList($id, $u);
				if(!isset($return['Error'])){
					$u = $GLOBALS['Db-I']->getUserById($u);
					echo $u->getName();
				} else echo 'Error:'.$return['Error'];	
				
			} else if(isset($_POST['action']) && $_POST['action']=='deleteUserFromList' &&isset($_POST['uID']) && isset($_POST['lID'])){ //-------------   Delete User from List
				$id = $_POST['lID'];
				$u = $_POST['uID'];
				$return = $GLOBALS['Db-I']->unshareList($id, $u);
				if(!isset($return['Error'])){
					echo 'ok';
				} else echo 'Error:'.$return['Error'];
						
			} else if(isset($_POST['action']) && $_POST['action']=='getReplys' && isset($_POST['mID'])){ 					//-------------   Get Replys
				$m = $GLOBALS['Db-I']->getMessage($_POST['mID'], true);
				$main = new ViewDescriptor('content/parts/message');
				$x = $m->getReplys();
				if(count($x) > 0) {
					foreach($x as $r){
						$rr = new SubViewDescriptor('message');
						$rr->addValue('id', $r->getId());
						$rr->addValue('title', $r->getTitle());
						$rr->addValue('text', nl2br($r->getText()));
						$rr->addValue('date', $r->getDate());
						$rr->addValue('a_id', $r->getAuthor()->getId());
						$rr->addValue('reply_count', $r->getReplyCount());
						$rr->addValue('a_name', $r->getAuthor()->getName());
						
						$main->addSubView($rr);
					}
				} else  $main->removeSubView('message');
				echo $main->render();
				
			} else if(isset($_POST['action']) && $_POST['action']=='sendReply' && isset($_POST['mID']) && isset($_POST['title']) && isset($_POST['text'])){//-------------   Save Reply
				$r = $GLOBALS['Db-I']->newMessage($_POST['title'], $_POST['text'], $_POST['mID']);
				if(!isset($r['Error'])){
					echo 'ok';
				} else echo 'Error:'.$r['Error'];
				
			} else if(isset($_POST['action']) && $_POST['action']=='getTodos' && isset($_POST['lID'])){//-------------   Load Todolist
				$page = isset($_POST['page']) ? $_POST['page'] : 1;
				$pp = TODOS_PER_PAGE;
				
				$p = $GLOBALS['Db-I']->getProject($_POST['lID']);
				if(!is_array($p)){
					$render = new ViewDescriptor('content/parts/todos_list');

					$render->addValue('id', $p->getId());
					$render->addValue('title', $p->getTitle());
					$render->addValue('desc', nl2br($p->getDescription()));
					$render->addValue('todo_count', $p->getTodoCount());
					
					if($p->getOwner()->getId() == $_SESSION['User']['id'] || $_SESSION['User']['group'] == 'admin'){
						$a = new SubViewDescriptor('authorized');
						$a->addValue('id', $p->getId());
						$render->addSubView($a);
					} else {
						$render->removeSubView('authorized');
					}
					
					$render->addValue('pagina', Template::getPagina($page, $pp, $p->getTodoCount(), 'javascript:loadTodos('.$_POST['lID'].', {@pp:page})'));
					$todos = $GLOBALS['Db-I']->getTodosForList($_POST['lID'], ($page-1)*$pp, $pp);
					if(count($todos) > 0){
						foreach($todos as $todo){
							// add todo to list
							$p_td = new SubViewDescriptor('todos');
							$p_tdS = new SubViewDescriptor('todos_script');
							
							if($todo->getCreator()->getId() == $_SESSION['User']['id'] || $_SESSION['User']['group'] == 'admin'){
								$a = new SubViewDescriptor('authorized1');
								$a->addValue('id', $todo->getId());
								$a->addValue('p_id', $p->getId());
								$p_td->addSubView($a);
							} else {
								$p_td->removeSubView('authorized1');
							}
							
							$p_tdS->addValue('id', $todo->getId());
							$p_tdS->addValue('p_id', $p->getId());
							
							$p_td->addValue('id', $todo->getId());
							$p_td->addValue('p_id', $p->getId());
							$p_td->addValue('tD', $todo->getTargetDate());
							
							$p_td->addValue('status', $todo->getStatus());
							$p_td->addValue('status_img', str_replace(' ', '_', $todo->getStatus()));
								
							$p_td->addValue('title', $todo->getTitle());
							$p_td->addValue('desc', Template::cutString($todo->getDescription(), 130));
							$p_td->addValue('creator', $todo->getCreator()->getName());
							
							$render->addSubView($p_td);
							$render->addSubView($p_tdS);
							
							unset($p_td);
							unset($p_tdS);
						}
					} else {
						$render->removeSubView('todos');
						$render->removeSubView('todos_script');
					}
					echo $render->render();
				} else echo 'Error:'.$r['Error'];
				
			} else if(isset($_POST['action']) && $_POST['action']=='getAllTodos'){//-------------   get All Todos
				$page = isset($_POST['page']) ? $_POST['page'] : 1;
				$pp = TODOS_PER_PAGE;
				$render = new ViewDescriptor('content/parts/todos_list_all');
			
				$todos_all = $GLOBALS['Db-I']->getTodos(($page-1)*$pp, $pp);
				$alltodos = 0;
				if(count($todos_all) > 0){
					foreach($todos_all as $todo){
						if($alltodos==0) $alltodos = $todo->getTodoCount();
						//add to all todos div
						$p_td_all = new SubViewDescriptor('todos_all');
						$p_tdS_all = new SubViewDescriptor('todos_script_all');
						
						if($todo->getCreator()->getId() == $_SESSION['User']['id'] || $_SESSION['User']['group'] == 'admin'){
							$a = new SubViewDescriptor('authorized1');
							$a->addValue('id', $todo->getId());
							$p_td_all->addSubView($a);
						} else {
							$p_td_all->removeSubView('authorized1');
						}
						
						$p_tdS_all->addValue('id', $todo->getId());
						
						$p_td_all->addValue('title', $todo->getTitle());
						$p_td_all->addValue('id', $todo->getId());
			
						$p_td_all->addValue('status', $todo->getStatus());
						$p_td_all->addValue('status_img', str_replace(' ', '_', $todo->getStatus()));
						
						$p_td_all->addValue('lId', '');
						$p_td_all->addValue('desc', Template::cutString($todo->getDescription(), 130));
						$p_td_all->addValue('creator', $todo->getCreator()->getName());
						
						$render->addSubView($p_td_all);
						$render->addSubView($p_tdS_all);
						
						unset($p_td_all);
						unset($p_tdS_all);
					}
				} else {
					$render->addValue('todo_count', 0);
					$render->removeSubView('todos_all');
					$render->removeSubView('todos_script_all');
				}
				$render->addValue('pagina', Template::getPagina($page, $pp, $alltodos, 'javascript:loadTodos(-100, {@pp:page})'));
				$render->addValue('all_todo_count', $alltodos);
				echo $render->render();
			}  else echo 'Error:'.ERROR_WRONG_ACTION;
		} else echo 'Error:'.ERROR_WRONG_SID;
	} else echo 'Error:'.ERROR_SESSION_TERMINATED;
?>